package com.caigou.controller;

import com.caigou.annotation.OperLog;
import com.caigou.bean.cms.param.*;
import com.caigou.bean.exam.entity.Users;
import com.caigou.bean.vo.*;
import com.caigou.constant.NewsConstant;
import com.caigou.enums.ResponseEnum;
import com.caigou.pojo.ChannelOtherConfigData;
import com.caigou.security.utils.CurrentUser;
import com.caigou.service.ChannelService;
import com.caigou.util.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(value = "频道类管理器(频道、栏目、板块管理/内容推送管理)")
@RestController
@RequestMapping("/channel")
public class ChannelController {

    @Resource
    private ChannelService channelService;

    @ApiOperation("获取频道列表资讯、推荐项(大部分是针对频道栏目名称、展示状态、展示数量等参数设置)")
    @RequestMapping("/loadChannelConfigList")
    @PreAuthorize("hasAnyAuthority('chakanpindaolanmuneirong')")
    public Result loadChannelConfigList(@RequestBody ChannelParam param){
        return channelService.loadChannelConfigList(param);
    }

    @ApiOperation("更新频道列表资讯、推荐项相关数据")
    @OperLog(operModule = NewsConstant.CHANNEL_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "更新频道列表资讯、推荐项相关数据")
    @RequestMapping("/updateChannelConfig")
    @PreAuthorize("hasAnyAuthority('lanmuyangshiguanli')")
    public Result updateChannelConfig(@RequestBody ChannelParam param){
        return channelService.updateChannelConfig(param);
    }


    @ApiOperation("大列表新增数据")
    @OperLog(operModule = NewsConstant.CHANNEL_MODULE, operType = NewsConstant.ADD_OPERATION, operDesc = "大列表新增数据")
    @RequestMapping("/saveChannelData")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result saveChannelData(@RequestBody List<ChannelDataParam> param){
        return channelService.saveChannelData(param);
    }

    @ApiOperation("大列表删除数据")
    @OperLog(operModule = NewsConstant.CHANNEL_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "大列表删除数据")
    @RequestMapping("/deleteChannelData")
    @PreAuthorize("hasAnyAuthority('yichupindaoneirong')")
    public Result deleteChannelData(@RequestBody ChannelDataParam param){
        return channelService.deleteChannelData(param);
    }

    @ApiOperation("大列表修改标题数据")
    @OperLog(operModule = NewsConstant.CHANNEL_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "大列表新增数据")
    @RequestMapping("/updateChannelData")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result updateChannelData(@RequestBody ChannelDataParam param){
        return channelService.updateChannelData(param);
    }

    @ApiOperation("大列表数据查询")
    @RequestMapping("/loadChannelDataList")
    @PreAuthorize("hasAnyAuthority('chakanpindaolanmuneirong')")
    public Result loadChannelDataList(@RequestBody ChannelDataListParam param){
        if (null == param.getPlateId()) return Result.error(ResponseEnum.PARAM_ERROR);
        return channelService.loadChannelDataList(param);
    }

    @ApiOperation("大列表置顶操作")
    @OperLog(operModule = NewsConstant.CHANNEL_MODULE, operType = NewsConstant.TOP_OPERATION, operDesc = "大列表置顶操作")
    @RequestMapping("/updateChannelDataStickStatus")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result updateChannelDataStickStatus(@RequestBody ChannelDataParam param){
        return channelService.updateChannelDataStickStatus(param);
    }

    @ApiOperation("大列表推荐操作")
    @OperLog(operModule = NewsConstant.CHANNEL_MODULE, operType = NewsConstant.RECOMMEND_OPERATION, operDesc = "大列表推荐操作")
    @RequestMapping("/updateChannelDataRecommendStatus")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result updateChannelDataRecommendStatus(@RequestBody ChannelDataParam param){
        return channelService.updateChannelDataRecommendStatus(param);
    }

    @ApiOperation("推荐列表数据查询")
    @RequestMapping("/loadChannelRecommendDataList")
    @PreAuthorize("hasAnyAuthority('chakanpindaolanmuneirong')")
    public Result loadChannelRecommendDataList(@RequestBody ChannelDataListParam param){
        if (null == param.getPlateId()) return Result.error(ResponseEnum.PARAM_ERROR);
        return channelService.loadChannelRecommendDataList(param);
    }

    @ApiOperation("推荐列表新增数据")
    @OperLog(operModule = NewsConstant.CHANNEL_MODULE, operType = NewsConstant.ADD_OPERATION, operDesc = "推荐列表新增数据")
    @RequestMapping("/saveChannelRecommendData")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result saveChannelRecommendData(@RequestBody ChannelDataParam param){
        return channelService.saveChannelRecommendData(param);
    }

    @ApiOperation("推荐列表修改标题数据")
    @OperLog(operModule = NewsConstant.CHANNEL_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "推荐列表修改标题数据")
    @RequestMapping("/updateChannelRecommendData")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result updateChannelRecommendData(@RequestBody ChannelDataParam param){
        return channelService.updateChannelRecommendData(param);
    }

    @ApiOperation("新增到推荐弹窗--(大列表推荐按钮、推荐列表新增，选择之前的弹窗)")
    @RequestMapping("/loadChannelTitleConfig")
    public Result loadChannelTitleConfig(@RequestBody ChannelDataParam param){
        return channelService.loadChannelTitleConfig(param);
    }

    @ApiOperation("新增到推荐弹窗--(大列表推荐按钮、推荐列表新增，选择文章之后获取弹窗的内容)")
    @RequestMapping("/loadChannelRecommendDataInfo")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result loadChannelRecommendDataInfo(@RequestBody ChannelDataParam param){
        return channelService.loadChannelRecommendDataInfo(param);
    }

    @ApiOperation("推荐位--更新推荐位数据锁定状态")
    @OperLog(operModule = NewsConstant.CHANNEL_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "推荐位--更新推荐位数据锁定状态")
    @RequestMapping("/updateChannelDataLockStatus")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result updateChannelDataLockStatus(@RequestBody ChannelDataParam param){
        return channelService.updateChannelDataLockStatus(param);
    }

    @ApiOperation("推荐位--更新推荐位数据展示顺序")
    @RequestMapping("/updateChannelDataShowIndex")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli')")
    public Result updateChannelDataShowIndex(@RequestBody List<ChannelDataParam> list){
        return channelService.updateChannelDataShowIndex(list);
    }

    @ApiOperation("获取频道列表其他设置数据获取(频道ID参数)")
    @RequestMapping("/loadChannelOtherConfig")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli','qitashezhi')")
    public Result loadChannelOtherConfig(@RequestBody ChannelParam param){
        return channelService.loadChannelOtherConfig(param);
    }

    @ApiOperation("频道列表其他设置数据更新--参数为频道的ID(101)")
    @OperLog(operModule = NewsConstant.CHANNEL_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "频道列表其他设置数据更新")
    @RequestMapping("/updateChannelOtherConfig")
    @PreAuthorize("hasAnyAuthority('pindaolanmuneirong3guanli','qitashezhi')")
    public Result updateChannelOtherConfig(@RequestBody ChannelOtherConfigData param){
        return channelService.updateChannelOtherConfig(param);
    }

    @ApiOperation("获取采集分站频道列表(最外层)")
    @RequestMapping("/loadCollectionSubstationChannelList")
    public Result loadCollectionSubstationChannelList(@CurrentUser Users users){
        return channelService.loadCollectionSubstationChannelList(users.getUserid());
    }

    @ApiOperation("推荐列表新增数据（分站）")
    @RequestMapping("/saveChannelRecommendDataByCollection")
    public Result saveChannelRecommendDataByCollection(@RequestBody ChannelDataParam param){
        return channelService.saveChannelRecommendData(param);
    }

    @ApiOperation("保存页面发布权限")
    @PostMapping("/staff/publish/save")
    @PreAuthorize("hasAnyAuthority('yuangong3guanli')")
    public Result savePublishPrivileges(@RequestBody StaffPublishParam param,  @CurrentUser Users user) {
        return channelService.savePublishPrivileges(param);
    }

    @ApiOperation("查询页面发布权限 用户发布权接口")
    @PostMapping("/publish/privileges")
    public Result findPublishPrivileges(@RequestBody StaffPublishParam param,@CurrentUser Users user) {
        return channelService.findPublishPrivileges(param);
    }

    @ApiOperation("查询页面发布权限 用户发布权接口")
    @PostMapping("/staff/publish/privileges")
    public Result findStaffPublishPrivileges(@CurrentUser Users user) {
        return channelService.findStaffPublishPrivileges(user.getUserid());
    }

    @ApiOperation("查询首页权限")
    @PostMapping("/staff/home/page")
    public Result findStaffPublishPrivileges(@RequestBody HomePageParam param,@CurrentUser Users user) {
        if (StringUtils.isEmpty(param.getPrivilegeName())) {
            return Result.error(ResponseEnum.PRIVILEGE_ERROR);
        }
        return channelService.findStaffHomePagePrivilege(param,user.getUserid());
    }

    @ApiOperation("查询用户频道栏目管理权限列表 分三层")
    @GetMapping("/staff/manage/privileges")
    public Result findStaffManagePrivilegesByUserId(@CurrentUser Users user) {
        return channelService.loadChannels(user.getUserid());
    }

    @ApiOperation("保存到用户频道栏目管理权限列表")
    @PostMapping("/staff/manage/save")
    public Result saveStaffManagePrivilegesByUserId(@RequestBody List<Integer> privilegeCodes ,@CurrentUser Users user) {
        return channelService.saveStaffManagePrivilegesByUserId(privilegeCodes,user.getUserid());
    }

    @ApiOperation("查询系统频道栏目设置 启用/禁用")
    @PostMapping("/section/status/query")
    public Result findSectionStatus(@RequestBody SectionStatusParam param, @CurrentUser Users user) {
        return channelService.findSectionStatus(param);
    }

    @ApiOperation("查询系统频道栏目设置 启用/禁用")
    @PostMapping("/section/status/update")
    public Result updateSectionStatus(@RequestBody SectionStatusParam param, @CurrentUser Users user) {
        return channelService.updateSectionStatus(param);
    }
}
